package com.chainyoung.component_tmmarket.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.appcommon.entity.MarketDetail;
import com.chainyoung.common.constant.CommonStringConstants;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.component_tmmarket.R;

import com.chainyoung.component_tmmarket.ui.MarketKLineActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import java.math.BigDecimal;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.CURRENCY_IN;
import static com.chainyoung.common.constant.CommonStringConstants.CURRENCY_OUT;

public class MarketListAdapter extends BaseQuickAdapter<MarketDetail, BaseViewHolder> {

    private ImageLoader mImageLoader;

    private onShowKLine onShowKLine;

    private String currencyOut;//交易区


    // private List<Kline> mDatas=new ArrayList<>();


    public MarketListAdapter(@Nullable List<MarketDetail> data, String currencyOut) {
        super(R.layout.tmmarket_list_market_item, data);
        mImageLoader = Utils.getAppComponent().imageLoader();
        this.currencyOut = currencyOut;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final MarketDetail item) {

        mImageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(item.getCoin_img())
                        .errorPic(R.mipmap.ic_default)
                        .imagerView((ImageView) (helper.getView(R.id.iv_logo)))
                        .build());

        boolean b = item.getChange().startsWith("-");
        boolean isZero = TextUtils.isEmpty(item.getChange().replace(",", "")) || (Double.valueOf(item.getChange().replace(",", "")) == 0);
        helper.setText(R.id.tv_name, item.getName().toUpperCase());
        helper.setText(R.id.tv_newest_price_dollar, StringUtil.subZeroAndDot(new BigDecimal(item.getLast()).setScale(6, BigDecimal.ROUND_DOWN) + ""));
        helper.setText(R.id.tv_newest_price_rmb, CommonStringConstants.RMB + new BigDecimal(item.getCny()).setScale(2, BigDecimal.ROUND_DOWN));
        helper.setText(R.id.tv_trade_amount, StringUtil.subZeroAndDot(new BigDecimal(item.getVol()).setScale(6, BigDecimal.ROUND_DOWN) + ""));
        helper.setText(R.id.tv_day_change, isZero ? item.getChange()+"%" : b ? item.getChange() + "%" : "+" + item.getChange() + "%");
        helper.setText(R.id.tv_market_name, "/" + currencyOut.toUpperCase());
        helper.setBackgroundRes(R.id.tv_day_change, isZero ? R.drawable.shape_market_green : !b ? R.drawable.shape_market_green :
                R.drawable.shape_market_red);
//        Drawable drawableUp = ContextCompat.getDrawable(mContext, R.mipmap.ic_change_up);
//        Drawable drawableDown = ContextCompat.getDrawable(mContext, R.mipmap.ic_change_down);
//        ((TextView) helper.getView(R.id.tv_day_change)).
//                setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, isZero ? null : !b ? DrawableTintUtil.tintDrawable(drawableUp, ContextCompat.getColor(mContext, R.color._09C173)) :
//                        DrawableTintUtil.tintDrawable(drawableDown, ContextCompat.getColor(mContext, R.color._F95A54)), null);

        helper.setText(R.id.tv_higest_price, item.getHigh());
        helper.setText(R.id.tv_lowest_price, item.getLow());
        helper.setText(R.id.tv_trade_volume, item.getLast());
        ConstraintLayout constraintLayout = helper.getView(R.id.cl_total);
        final ConstraintLayout clDetail = helper.getView(R.id.cl_detail);

        constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MarketKLineActivity.class);
                intent.putExtra(CommonStringConstants.CURRENCY_IN, item.getName());
                intent.putExtra(CommonStringConstants.CURRENCY_OUT, currencyOut);
                mContext.startActivity(intent);

            }
        });

        helper.getView(R.id.tv_trade_now).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterUtils.getInstance()
                        .build(RouterConstants.PATH_MAIN_MARKET_ACTIVITY)
                        .withInt("page", 1)
                        .withString(CURRENCY_OUT, currencyOut)
                        .withString(CURRENCY_IN, item.getName())
                        .navigation(mContext);
            }
        });

    }

    public void setOnShowKLine(MarketListAdapter.onShowKLine onShowKLine) {
        this.onShowKLine = onShowKLine;
    }


    public interface onShowKLine {
        void onshowKline(int position);
    }


}
